<template>
  <div>
    <el-container style="height:700px; border: 1px solid #eee">
      <el-header style="font-size: 40px; background-color: white;">威海环海自驾汽车租凭管理系统</el-header>
      <el-container>
        <el-aside width="230px" style="border: 1px solid #eee">
          <el-menu :default-openeds="['1', '3']">

            <el-submenu index="1">
              <template slot="title"><i class="el-icon-message"></i>系统信息管理</template>
              <el-menu-item-group>
                <el-menu-item index="1-1">
                  <router-link :to="{ path: '/admin/clientInfo', query: { userAcct: $route.query.userAcct } }">客户信息管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-2">
                  <router-link :to="{ path: '/admin/clientAcct', query: { userAcct: $route.query.userAcct } }">客户账户管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-3">
                  <router-link :to="{ path: '/admin/clientlogin', query: { userAcct: $route.query.userAcct } }">客户登录管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-4">
                  <router-link :to="{ path: '/admin/saleInfo', query: { userAcct: $route.query.userAcct } }">业务员信息管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-5">
                  <router-link :to="{ path: '/admin/salelogin', query: { userAcct: $route.query.userAcct } }">业务员登录管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-6">
                  <router-link :to="{ path: '/admin/managerInfo', query: { userAcct: $route.query.userAcct } }">经理信息管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-7">
                  <router-link :to="{ path: '/admin/managerlogin', query: { userAcct: $route.query.userAcct } }">经理登录管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-8">
                  <router-link :to="{ path: '/admin/adminInfo', query: { userAcct: $route.query.userAcct } }">管理员信息管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-9">
                  <router-link :to="{ path: '/admin/adminlogin', query: { userAcct: $route.query.userAcct } }">管理员登录管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-10">
                  <router-link :to="{ path: '/admin/carInfo', query: { userAcct: $route.query.userAcct } }">车辆信息管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-11">
                  <router-link :to="{ path: '/admin/leaseInfo', query: { userAcct: $route.query.userAcRct } }">租凭登记信息管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-12">
                  <router-link :to="{ path: '/admin/returnInfo', query: { userAcct: $route.query.userAcct } }">租凭归还信息管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-13">
                  <router-link :to="{ path: '/admin/rechargeInfo', query: { userAcct: $route.query.userAcct } }">充值信息管理</router-link>
                </el-menu-item>
                <el-menu-item index="1-14">
                  <router-link :to="{ path: '/admin/deductInfo', query: { userAcct: $route.query.userAcct } }">扣款信息管理</router-link>
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>

          </el-menu>
        </el-aside>

        <el-main>
          <!-- 表单 -->
          <el-form :inline="true" :model="searchForm" class="demo-form-inline">
            <el-form-item label="客户姓名">
              <el-input v-model="searchForm.clientName" placeholder="客户姓名"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleAdd">添加</el-button>
            </el-form-item>
          </el-form>

          <!-- 隐藏的编辑对话框 -->
          <el-dialog title="编辑扣款信息" :visible.sync="dialogVisible">
            <el-form :model="editForm" label-width="100px">
              <el-form-item label="租凭单号">
                <el-input v-model="editForm.checkID" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="客户编号">
                <el-input v-model="editForm.clientID"></el-input>
              </el-form-item>
              <el-form-item label="客户姓名">
                <el-input v-model="editForm.clientName"></el-input>
              </el-form-item>
              <el-form-item label="扣款金额">
                <el-input v-model="editForm.demoney"></el-input>
              </el-form-item>
              <el-form-item label="扣款时间">
                <el-date-picker v-model="editForm.detime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="handleEdit">确定</el-button>
              <el-button @click="dialogVisible = false">取消</el-button>
            </div>
          </el-dialog>

          <!-- 新增扣款信息的对话框 -->
          <el-dialog title="添加扣款信息" :visible.sync="addDialogVisible">
            <el-form :model="editForm" label-width="100px">
              <el-form-item label="租凭单号">
                <el-input v-model="editForm.checkID"></el-input>
              </el-form-item>
              <el-form-item label="客户编号">
                <el-input v-model="editForm.clientID"></el-input>
              </el-form-item>
              <el-form-item label="客户姓名">
                <el-input v-model="editForm.clientName"></el-input>
              </el-form-item>
              <el-form-item label="扣款金额">
                <el-input v-model="editForm.demoney"></el-input>
              </el-form-item>
              <el-form-item label="扣款时间">
                <el-date-picker v-model="editForm.detime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="handleAddSave">确定</el-button>
              <el-button @click="addDialogVisible = false">取消</el-button>
            </div>
          </el-dialog>

          <!-- 表格 -->
          <el-table :data="tableData" border>
            <el-table-column prop="checkID" label="租凭单号" width="auto"></el-table-column>
            <el-table-column prop="clientID" label="客户编号" width="auto"></el-table-column>
            <el-table-column prop="clientName" label="客户姓名" width="auto"></el-table-column>
            <el-table-column prop="demoney" label="扣款金额" width="80px"></el-table-column>
            <el-table-column prop="detime" label="扣款时间" width="110px"></el-table-column>
            <el-table-column label="操作" v-slot="scope">
              <el-button type="primary" size="mini" @click="handleEditClick($event, scope)">编辑</el-button>
              <el-button type="danger" size="mini" @click="handleDeleteClick(scope.row.checkID)">删除</el-button>
            </el-table-column>
          </el-table>

          <br><br>

        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import axios from 'axios';
import qs from "qs";

export default {
  data() {
    return {
      tableData: [],
      searchForm:{
        clientName:""
      },
      dialogVisible: false, // 对话框是否可见
      addDialogVisible: false,
      editForm: {           // 要编辑扣款信息
        checkID:'',
        clientID:'',
        clientName:'',
        demoney:'',
        detime:''
      },
    }
  },
  methods: {
    onSubmit: function() {
      axios.get("http://localhost:8081/deductInfo/select3", {
        params: {
          clientName: this.searchForm.clientName
        }
      })
          .then((result) => {
            this.tableData = result.data.data;
          })
          .catch((error) => {
            console.log(error);
          });
    },
    handleSizeChange: function (val) {
      alert("每页记录数变化" + val);
    },
    handleCurrentChange: function (val) {
      alert("页码发生变化" + val);
    },
    handleAdd() {
      this.addDialogVisible = true;
      this.editForm = {
        checkID:'',
        clientID:'',
        clientName:'',
        demoney:'',
        detime:''
      };
    },
    handleAddSave() {
      const data = {
        checkID: this.editForm.checkID,
        clientID: this.editForm.clientID,
        clientName: this.editForm.clientName,
        demoney: this.editForm.demoney,
        detime: this.editForm.detime
      };
      axios.post('http://localhost:8081/deductInfo/insert', data)
          .then(response => {
            console.log(response);
            this.$message.success('添加成功');
            this.addDialogVisible = false;
            // 新增成功后，重新获取数据刷新表格
            this.getTableData();
          })
          .catch(error => {
            console.log(error);
          });
    },
    handleEditClick(event, scope) {
      console.log(scope.row);
      this.editForm = { ...scope.row };
      this.dialogVisible = true;
    },
    handleEdit() {
      const data = {
        checkID: this.editForm.checkID,
        clientID: this.editForm.clientID,
        clientName: this.editForm.clientName,
        demoney: this.editForm.demoney,
        detime: this.editForm.detime
      };
      axios.post('http://localhost:8081/deductInfo/update', data)
          .then(response => {
            console.log(response);
            this.$message.success('编辑成功');
            this.dialogVisible = false;
            // 更新成功后，重新获取数据刷新表格
            this.getTableData();
          })
          .catch(error => {
            console.log(error);
          });
    },
    // 获取扣款信息的数据并刷新表格
    getTableData() {
      axios.get("http://localhost:8081/deductInfo/select2").then((result) => {
        this.tableData = result.data.data;
      });
    },
    handleDeleteClick(checkID) {
      this.$confirm('确认删除该扣款信息吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 用户点击了“确认”按钮
        const data = qs.stringify({ checkID: checkID });
        axios.post('http://localhost:8081/deductInfo/delete', data, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        })
            .then(response => {
              console.log(response);
              this.$message.success('删除成功');
              this.getTableData(); // 删除成功后，重新获取数据刷新表格
            })
            .catch(error => {
              console.log(error);
            });
      }).catch(() => {
        // 用户点击了“取消”按钮
      });
    }
  },
  mounted() {
    //发送异步请求，获取数据
    axios.get("http://localhost:8081/deductInfo/select2").then((result) => {
      this.tableData=result.data.data;
    });
  },

}
</script>

<style>

</style>